package net.bplaced.streetfighter.fragment;

import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.lib.utils.StreetfighterUtils;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.Toast;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class PreferencesFragment
        extends PreferenceFragment
        implements OnSharedPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.addPreferencesFromResource(R.xml.preferences);

        this.initializeSummaries();
    }

    @Override
    public void onResume() {

        super.onResume();
        PreferenceManager.getDefaultSharedPreferences(this.getActivity())
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {

        super.onPause();
        PreferenceManager.getDefaultSharedPreferences(this.getActivity()).unregisterOnSharedPreferenceChangeListener(
                this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key.equals(this.getResources().getString(R.string.const_pref_key_reply_user))) {
            final Preference replyUserName = this.findPreference(key);

            final String summaryString =
                    new StringBuilder()
                            .append(this.getActivity().getResources().getString(R.string.pref_reply_user_summary_set))
                            .append(" ")
                            .append(StreetfighterUtils.getPreferenceString(this.getActivity(), this.getResources()
                                    .getString(R.string.const_pref_key_reply_user), "")).toString();

            replyUserName.setSummary(summaryString);
        }

        if (key.equals(this.getResources().getString(R.string.const_pref_key_auto_update_check))) {
            final Preference autoUpdateCheck = this.findPreference(key);

            String summaryString = "";

            if (StreetfighterUtils.getPreferenceBoolean(this.getActivity(), key, false)) {

                summaryString =
                        this.getActivity().getResources().getString(R.string.pref_auto_update_check_summary_true);
            } else {

                summaryString =
                        this.getActivity().getResources().getString(R.string.pref_auto_update_check_summary_false);
            }

            autoUpdateCheck.setSummary(summaryString);

            Toast.makeText(this.getActivity(),
                    this.getActivity().getResources().getString(R.string.pref_change_notify), Toast.LENGTH_SHORT)
                    .show();
        }

        if (key.equals(this.getResources().getString(R.string.const_pref_key_update_notification))) {
            final Preference updateNotificationType = this.findPreference(key);

            String summaryString = "";

            if (StreetfighterUtils.getPreferenceBoolean(this.getActivity(), key, true)) {

                summaryString =
                        this.getActivity().getResources()
                                .getString(R.string.pref_update_notification_type_summary_true);
            } else {

                summaryString =
                        this.getActivity().getResources()
                                .getString(R.string.pref_update_notification_type_summary_false);
            }

            updateNotificationType.setSummary(summaryString);
        }
    }

    private void initializeSummaries() {

        // Auto update check
        String key = this.getResources().getString(R.string.const_pref_key_auto_update_check);

        final Preference autoUpdateCheck = this.findPreference(key);

        String summaryString = "";

        if (StreetfighterUtils.getPreferenceBoolean(this.getActivity(), key, false)) {

            summaryString = this.getActivity().getResources().getString(R.string.pref_auto_update_check_summary_true);
        } else {

            summaryString = this.getActivity().getResources().getString(R.string.pref_auto_update_check_summary_false);
        }

        autoUpdateCheck.setSummary(summaryString);

        // Update notification type
        key = this.getResources().getString(R.string.const_pref_key_update_notification);

        final Preference updateNotificationType = this.findPreference(key);

        summaryString = "";

        if (StreetfighterUtils.getPreferenceBoolean(this.getActivity(), key, true)) {

            summaryString =
                    this.getActivity().getResources().getString(R.string.pref_update_notification_type_summary_true);
        } else {

            summaryString =
                    this.getActivity().getResources().getString(R.string.pref_update_notification_type_summary_false);
        }

        updateNotificationType.setSummary(summaryString);

        // Reply user name
        key = this.getResources().getString(R.string.const_pref_key_reply_user);

        final Preference replyUserName = this.findPreference(key);

        summaryString =
                new StringBuilder()
                        .append(this.getActivity().getResources().getString(R.string.pref_reply_user_summary_set))
                        .append(" ").append(StreetfighterUtils.getPreferenceString(this.getActivity(), key, ""))
                        .toString();

        replyUserName.setSummary(summaryString);
    }
}